/**
 * @filename			window.js
 * @author				zfkun <zfkun@163.com>
 * @copyright 			ku6.com
 * @datetime			2006-12-21
 * @lastmodify			2007-5-24
 */
function winForm(){
	var titile = '';
	var width = 260;
	var height = 90;
	var path = Common.url.img + "common/window/";
	var sClose = '<input type="image" id="dialogBoxClose" onclick="new winForm().close();" src="'+ path +'close_btn.gif" border="0" align="absmiddle" />';
	var sBody = '<div id="dialogbodyBox"><div id="dialogMsg" style="font-size:12px;"></div></div>';
	var sBox = '\
		<table id="dialogBox" width="' + width + '" border="0" cellpadding="0" cellspacing="0" style="border:1px solid #335D55;display:none;z-index:10;">\
		<tr height="1" bgcolor="#D6E3EB"><td></td></tr>\
		<tr height="25">\
		<td background="' + path + 'table_bg.jpg"  bgcolor="#CFDBD9">\
			<table onselectstart="return false;" style="-moz-user-select:none;" width="100%" border="0" cellpadding="0" cellspacing="0">\
			<tr>\
			<td width="6"></td>\
			<td id="dialogBoxTitle" onmousedown="new winForm().moveStart(event, \'dialogBox\')" style="color:#335D55;cursor:move;font-size:12px;font-weight:bold;"></td>\
			<td id="dialogClose" width="27" align="right" valign="middle">\
				' + sClose + '\
			</td>\
			</tr>\
			</table>\
		</td>\
		</tr>\
		<tr id="dialogHeight" style="height:' + height + '">\
			<td id="dialogBody" bgcolor="#ffffff">' + sBody + '</td>\
		</tr>\
		</table>\
		<div id="dialogBoxShadow" style="display:none;z-index:2;"></div>\
	';

	var sBG = '\
		<div id="dialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:100%;background:url(' + path + 'blank.gif);opacity:0.5;filter:Alpha(Opacity=50);z-index:10;"></div>\
	';

	this.show = function(){
		this.center();
		this.shadow();
	}
	this.close = function(){
		$('dialogBox').style.display='none';
		$('dialogBoxBG').style.display='none';
		$('dialogBoxShadow').style.display = "none";
		$('dialogBody').innerHTML = sBody;
		this._changeSelect(true);
	}

	this.init = function(){
		$('dialogCase') ? $('dialogCase').parentNode.removeChild($('dialogCase')) : function(){};
		var oDiv = document.createElement('span');
		oDiv.id = "dialogCase";
		oDiv.innerHTML = sBG + sBox;
		document.body.appendChild(oDiv);
		$('dialogBoxBG').style.height = document.body.scrollHeight;
	}
	this.button = function(_sId, _sFuc){
		if($(_sId)){
			$(_sId).style.display = '';
			if($(_sId).addEventListener){
				if($(_sId).act){$(_sId).removeEventListener('click', function(){eval($(_sId).act)}, false);}
				$(_sId).act = _sFuc;
				$(_sId).addEventListener('click', function(){eval(_sFuc)}, false);
			}else{
				if($(_sId).act){$(_sId).detachEvent('onclick', function(){eval($(_sId).act)});}
				$(_sId).act = _sFuc;
				$(_sId).attachEvent('onclick', function(){eval(_sFuc)});
			}
		}
	}
	this.shadow = function(){
		var oShadow = $('dialogBoxShadow');
		var oDialog = $('dialogBox');
		oShadow['style']['position'] = "absolute";
		oShadow['style']['background']	= "#000";
		oShadow['style']['display']	= "";
		oShadow['style']['opacity']	= "0.2";
		oShadow['style']['filter'] = "alpha(opacity=20)";
		oShadow['style']['top'] = oDialog.offsetTop + 6;
		oShadow['style']['left'] = oDialog.offsetLeft + 6;
		oShadow['style']['width'] = oDialog.offsetWidth;
		oShadow['style']['height'] = oDialog.offsetHeight;
	}
	this.set = function(_oAttr, _sVal){
		if(_sVal != ''){
			switch(_oAttr){
				case 'title':
					$('dialogBoxTitle').innerHTML = _sVal;
					title = _sVal;
					break;
				case 'content':
					$('dialogMsg').innerHTML = _sVal;
					break;
				case 'width':
					$('dialogBox')['style']['width'] = _sVal;
					width = _sVal;
					break;
				case 'height':
					$('dialogHeight')['style']['height'] = _sVal;
					height = _sVal;
					break;
			}
		}
		this.center();
		this.shadow();
	}

	this.moveStart = function (event, _sId){
		var oObj = $(_sId);
		oObj.onmousemove = mousemove;
		oObj.onmouseup = mouseup;
		oObj.setCapture ? oObj.setCapture() : function(){};
		oEvent = event || window.event;
		var dragData = {x : oEvent.clientX, y : oEvent.clientY};
		var backData = {x : parseInt(oObj.style.top), y : parseInt(oObj.style.left)};

		function mousemove(_ev){
			var oEvent = _ev || window.event;
			var iLeft = oEvent.clientX - dragData["x"] + parseInt(oObj.style.left);
			var iTop = oEvent.clientY - dragData["y"] + parseInt(oObj.style.top);
			oObj.style.left = iLeft+"px";
			oObj.style.top = iTop+"px";
			$('dialogBoxShadow').style.left = (iLeft + 6)+"px";
			$('dialogBoxShadow').style.top = (iTop + 6)+"px";
			dragData = {x: oEvent.clientX, y: oEvent.clientY};
		}
		function mouseup(_ev){
			var oEvent = _ev || window.event;
			oObj.onmousemove = null;
			oObj.onmouseup = null;
			if(oEvent.clientX < 1 || oEvent.clientY < 1 || oEvent.clientX > document.body.clientWidth || oEvent.clientY > document.body.clientHeight){
				oObj.style.left = backData.y+"px";
				oObj.style.top = backData.x+"px";
				$('dialogBoxShadow').style.left = backData.y + 6+"px";
				$('dialogBoxShadow').style.top = backData.x + 6+"px";
			}
			oObj.releaseCapture ? oObj.releaseCapture() : function(){};
		}
		function fixE(ig_){
			if (typeof ig_ == "undefined"){
				ig_ = window.event;
			}
			if (typeof ig_.layerX == "undefined"){
				ig_.layerX = ig_.offsetX;
			}
			if (typeof ig_.layerY == "undefined"){
				ig_.layerY = ig_.offsetY;
			}
			if (typeof ig_.which == "undefined"){
					ig_.which = ig_.button;
			}
			return ig_;
		}		
	}
	this.center = function(){
		var _o = $('dialogBox');
		var _b = document.body;
		_o['style']['display'] = '';
		_o['style']['position'] = "absolute";
		_o['style']['left'] = (_b.clientWidth / 2) - (_o.offsetWidth / 2) + "px";
		_o['style']['top'] = Math.ceil((_b.clientHeight - _o.offsetHeight)/2)*0.6 + _b.scrollTop + "px";
	}
	this.custom = function(title,msg,okEvent){
		this.init();
		this._changeSelect(false);
		this.set('title',title);
		this.set('content',msg);
		this.button("dialogBoxClose",okEvent);
		this.show();
	}
	this._changeSelect = function(flg) {
		if(typeof(flg)=='boolean' && flg==false){
			var _saveSizeForFF = true;
			var _display = 'none';	var _method = 'doPause';
		}else{
			var _saveSizeForFF = false;
			var _display = '';	var _method = 'doPlay';
		}
		if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) {
			var _inputs = document.getElementsByTagName("select");
			 for(var i=0;i<_inputs.length;i++){
				var vi = _inputs.item(i);
				//if(vi.type.toLowerCase() == "select"){
					vi.style.display = _display;
				//}
			 }

			var _object = document.getElementsByTagName("object");
			for(var i=0;i<_object.length;i++){
				var vo = _object.item(i);
				vo.style.display = _display;
				var _oid = vo.id ? vo.id : vo.name;
				try{
					if(_oid == 'video_player'){
						vo.SetVariable('PlayListener', _method);
					}
				}catch(e){}
			}
		}else{
			var _embed = document.getElementsByTagName("embed");
			for(var i=0;i<_embed.length;i++){
				var _e = _embed.item(i);
				if(_saveSizeForFF){
					_e.oldwidth = _e.width;
					_e.oldheight= _e.height;
					_e.width  = 1;
					_e.height = 1;
				}else{
					_e.width = _e.oldwidth;
					_e.height= _e.oldheight;
				}
				var _eid = _e.id ? _e.id : _e.name;
				try{
					if(_eid == 'video_player'){_embed.SetVariable('PlayListener', _method);}
				}catch(e){}
			}
		}
	}
}